<template>
  <div>
    <!-- <h3>list组件</h3> -->
    <!-- row-key一行的唯一标识 default-expand-all默认展开所有 -->
    <el-table
      :data="list"
      style="width: 100%"
      row-key="id"
      border
      :tree-props="{ children: 'children' }"
    >
      <el-table-column prop="id" label="用户编号"> </el-table-column>
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <el-table-column prop="rolename" label="所属角色"> </el-table-column>

      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-button type="primary" v-if="scope.row.status == 1"
            >启用</el-button
          >
          <el-button type="info" v-else>禁用</el-button>
        </template>
      </el-table-column>

      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope)">编辑</el-button>
          <el-button type="danger" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { requserdelete } from "../../../http/api";
import { successAlert } from "../../../utils/alert";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["list"],
  data() {
    return {};
  },

  methods: {
    ...mapActions({
      changeInfo: "changeInfo",
    }),

    handleEdit(scoped) {
      console.log(scoped.row);
      // scoped.$index下标
      // scoped.row那条数据
      this.$emit("willUpdate", scoped.row.uid);
    },
    handleDelete(scope) {
      this.$confirm("是否删除该数据?", "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 开始删除,发送请求
          requserdelete({
            uid: scope.row.uid,
          }).then((res) => {
            if (res.data.code == 200) {
              successAlert(res.data.msg);
              if (scope.row.uid == this.info.uid) {
                // 清空数据
                this.changeInfo({});
                // 退出登录
                this.$router.replace("/login");
                return;
              }
              this.$emit("init");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  computed: {
    ...mapGetters(["info"]),
  },
};
</script>

<style>
</style>